import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/response_data.dart';
import 'package:ls_work/net/response/service_execute/service_execute_old_data.dart';

class ServiceExecuteHomeViewModel extends BaseViewModel{
  final String? scanCode;
  final String? customerElderId;
  ServiceExecuteHomeViewModel(BuildContext context,{this.scanCode,this.customerElderId}) : super(context);

  var date = DateTime.now().toDateStr([yyyy,'-',mm,'-',dd]);

  ServiceExecuteOldData? oldData;

  var isBatchClick = false;

  void batchClick(){
    isBatchClick = !isBatchClick;
    notifyListeners();
    sendMessage(SendMessageEvent(MessageConfig.ENABLE_BATCH,obj: isBatchClick));
  }

  Future<void> getOldInfo() async {
    if(oldData==null){
      showLoadingState();
    }
    try{
      var data = await PostRequest.oldServiceStatistics(scanCode, date,customerElderId);
      if("0"==data['code']){
        oldData = ServiceExecuteOldData.fromJson(data['data']);
        notifyListeners();
        showContent();
      }else{
        showToast(data['msg']);
        finish();
      }
    }catch(e){
      showToast(NetErrorUtils.getNetError(e));
      finish();
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    getOldInfo();
  }

  @override
  void receiveMessage(SendMessageEvent event) {
    super.receiveMessage(event);
    if(event.msgCode==MessageConfig.REFRESH_PLAN_STATISTICS){
      getOldInfo();
    }
  }
}